package com.easy.customer.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.CustomerContacts;
import com.easy.common.service.ICustomerContactsService;
import com.easy.customer.mapper.CustomerContactsMapper;

public class CustomerContactsService extends ServiceImpl<CustomerContactsMapper,CustomerContacts> implements ICustomerContactsService {

    @Override
    public CustomerContacts insert(CustomerContacts contacts) {
        super.save(contacts);
        CustomerContacts result=super.getById(contacts.getId());
        return result;
    }

    @Override
    public CustomerContacts update(CustomerContacts contacts) {
        super.updateById(contacts);
        CustomerContacts result=super.getById(contacts.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        super.removeById(id);
        return 1;
    }

    @Override
    public int setPrimary(Integer id) {
        CustomerContacts contacts=super.getById(id);
        Integer customer_id=contacts.getCustomer_id();
        LambdaUpdateWrapper<CustomerContacts> wrapper=new LambdaUpdateWrapper<CustomerContacts>();
        wrapper.set(CustomerContacts::getIs_primary,0);
        wrapper.eq(CustomerContacts::getCustomer_id,customer_id);
        super.update(wrapper);
        wrapper=new LambdaUpdateWrapper<CustomerContacts>();
        wrapper.set(CustomerContacts::getIs_primary,1);
        wrapper.eq(CustomerContacts::getId,id);
        return 1;
    }
}
